<template>
  <el-dialog
      :title="!dataForm.id ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible"
      width="50%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="100px">
      <el-form-item label="活动名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="活动名称"></el-input>
      </el-form-item>
      <el-form-item label="参与人数" prop="peopleNum">
        <el-input-number v-model="dataForm.peopleNum" :min="1"  placeholder="参与人数"></el-input-number>
        
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
            v-model="dataForm.startTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            style="width: 600px"
            type="datetime"
            placeholder="开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
            v-model="dataForm.endTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            style="width: 600px;"
            type="datetime"
            placeholder="结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="活动负责人" prop="resFellowId">
        <el-select v-model="dataForm.resFellowId" style="width: 600px;" filterable clearable placeholder="活动负责人">
          <el-option v-for="item in fellows" :key="item.id" :label="item.name + ' ' + item.tel" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="参与人员" prop="fellowIds">
        <el-select v-model="dataForm.fellowIds" style="width: 600px;" filterable multiple clearable placeholder="参与人员">
          <el-option v-for="item in fellows" :key="item.id" :label="item.name + ' ' + item.tel" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="帮助对象" prop="recordlist">
        <el-select v-model="dataForm.recipientIds" style="width: 600px;" filterable multiple clearable placeholder="帮助对象">
          <el-option v-for="item in recipients" :key="item.id" :label="item.name + ' ' + item.categoryName+' '+item.age" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import service from "@/utils/httpRequest";
import request from "@/utils/httpRequest";

export default {
  data() {
    return {
      fellows: [
        {
          id: 0,
          name: '',
          tel: ''
        }
      ],
      recipients:[
        {
          id: 0,
          name: '',
          categoryName:'',
          gender:'',
          age:''
        }
      ],
      visible: false,
      dataForm: {
        id: 0,
        name: '',
        startTime: '',
        endTime: '',
        peopleNum: 0,
        resFellowId: null,
        fellowIds: [],
        recipientIds: []
      },
      dataRule: {
        name: [
          {required: true, message: '活动名称不能为空', trigger: 'blur'}
        ],
        startTime: [
          {required: true, message: '开始时间不能为空', trigger: 'blur'}
        ],
        endTime: [
          {required: true, message: '结束时间不能为空', trigger: 'blur'}
        ],
        peopleNum: [
          {required: true, message: '参与人数不能为空', trigger: 'blur'}
        ],
        resFellowId: [
          {required: true, message: '活动负责人不能为空', trigger: 'blur'}
        ],
        fellowIds: [
          {required: true, message: '参与活动会员不能为空', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    getRecipients(){
      request.get('/lovehelp/recipient/allrecipient').then((response) => {
        if (response && response.code === 200) {
          this.recipients = response.recipients
        } else {
          this.recipients = []
        }
      }).catch((error) => {
        console.error(error)
      })
    },
    getFellows() {
      request.get('/lovehelp/fellow/allFellowInfo').then((response) => {
        if (response && response.code === 200) {
          this.fellows = response.fellowVoInfo
        } else {
          this.fellows = []
        }
      }).catch((error) => {
        console.error(error)
      })
    },
    init(id) {
      this.getRecipients()
      this.getFellows();
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          service({
            method: 'GET',
            url: `/lovehelp/activity/info/${this.dataForm.id}`,
          }).then(res => {
            if (res.code === 200) {
              this.dataForm = res.activity
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          service({
            method: 'POST',
            url: `/lovehelp/activity/${!this.dataForm.id ? 'save' : 'update'}`,
            data: {
              id: this.dataForm.id,
              name: this.dataForm.name,
              startTime: this.dataForm.startTime,
              endTime: this.dataForm.endTime,
              peopleNum: this.dataForm.peopleNum,
              resFellowId: this.dataForm.resFellowId,
              fellowIds: this.dataForm.fellowIds,
              recipientIds: this.dataForm.recipientIds,
            }
          }).then(res => {
            if (res.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    }
  }
}
</script>
